//
//  NSString+Users.m
//  lifestyle
//
//  Created by wd on 15/8/20.
//  Copyright (c) 2015年 Wei Chuang Le ,Ltd. All rights reserved.
//

#import "NSString+Users.h"

@implementation NSString (Users)

- (NSString *)phonetics
{
    NSMutableString *source = [self mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    NSString *capsString = [source stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    return capsString;
}

+ (NSString *)stringWith:(NSString*)sourceString Chartype:(CharType)charType
{
    NSMutableString *source = [sourceString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    NSString *capsString = [source stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    switch (charType) {
        case CharType_UppercaseString: {
            capsString = [capsString uppercaseString];
            break;
        }
        case CharType_LowercaseString: {
            capsString = [capsString lowercaseString];
            break;
        }
        case CharType_CapitalizedFirstChar: {
            capsString = [capsString capitalizedString];
            break;
        }
        default: {
            break;
        }
    }
    return capsString;
}

+ (CGSize)sizeWithText:(NSString *)text andFont:(UIFont *)font andMaxsize: (CGSize)maxSize
{
    NSDictionary *arrts = @{NSFontAttributeName: font};
    
//    if (iOS7) {
        return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:arrts context:nil].size;
//    } else {
//        return [text sizeWithFont:font];
//    }
}

- (CGSize)sizeWithFont:(UIFont *)font andMaxsize:(CGSize)maxSize
{
    NSDictionary *arrts = @{NSFontAttributeName: (font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]])};
//    if (iOS7) {
        return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:arrts context:nil].size;
//    } else {
//        return [self sizeWithFont:font];
//    }
}

+ (BOOL)checkURL:(NSString *)urlString
{
    NSString *matchUrlStr = urlString;
    if (![urlString hasPrefix:@"http"]) {
        matchUrlStr = [NSString stringWithFormat:@"http://www.%@",urlString];
    }
     NSString *urlRegex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(m.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",urlRegex];
    
    BOOL isUrl = [urlTest evaluateWithObject:matchUrlStr];
    return isUrl;
}

- (BOOL)containsDesignatedString:(NSString *)aString
{
    NSRange range = [self rangeOfString:aString];
    return range.length > 0;
}

@end
